package com.hzdl.hotel.util;


import com.hzdl.hotel.bean.Check;
import com.hzdl.hotel.util.ZXingUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RentExcelUtil {

    /**
     * 使用出租单信息数据，显示到Excel中,返回ByteArrayOutputStream
     */
    public static ByteArrayOutputStream createExcelByRent(Check check, String templatePath ){
        try {

            // 根据参数获取输入流对象
            InputStream is = RentExcelUtil.class.getClassLoader().getResourceAsStream(templatePath);
            // 创建Excel对象
            HSSFWorkbook workbook = new HSSFWorkbook(is);

            // 获取sheet对象  (循环遍历省略，我们只需要用第一个sheet)
            HSSFSheet sheet =  workbook.getSheetAt(0);
            // 获取Row对象
            // 第一行
            int rownum =0;
            HSSFRow row1 =  sheet.getRow(rownum);
            HSSFCell row1_cell1 =  row1.getCell(0);
            row1_cell1.setCellValue(check.getLoginName()+"的流水单信息");

            // 第二行
            rownum++;
            HSSFRow row2 =  sheet.getRow(rownum);
            HSSFCell row2_cell2 = row2.getCell(1);
            row2_cell2.setCellValue(check.getCheckId());

            // 获取二维码
            InputStream logoStream=  RentExcelUtil.class.getClassLoader().getResourceAsStream("default.png");
            BufferedImage image =  ZXingUtil.createZXingCode(check.getCheckId());

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // 把二维码转换为输出流对象
            ImageIO.write(image,"JPEG",bos);

            // 把二维码设置到excel中
            //画图的顶级管理器，一个sheet只能获取一个
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

            /**
             * 参数4  设置图片的平铺程度  最大值是255  255代表铺满当前单元格 小于255就铺不满
             * 参数5  列的开始坐标
             * 参数6  行的开始坐标
             * 参数7  列的结束坐标
             * 参数8  行的结束坐标
             */
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, (short) 4, 1, (short) 5, 1);
            anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
            patriarch.createPicture(anchor,workbook.addPicture(bos.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));

//            HSSFCell row2_cell5 = row2.getCell(4);
//            row2_cell5.setCellValue("");// TODO 二维码显示位置
            //获取二维码
            BufferedImage bufferedImage = ZXingUtil.createZXingCode(check.getCheckId());
            // 第三行
            rownum++;
            HSSFRow row3 =  sheet.getRow(rownum);
            HSSFCell row3_cell2 = row3.getCell(1);
            row3_cell2.setCellValue(check.getLoginName());

//            HSSFCell row3_cell5 = row3.getCell(4);
//            row3_cell5.setCellValue(check.getLoginName());


            // 第四行
            rownum++;
            HSSFRow row4 =  sheet.getRow(rownum);
            HSSFCell row4_cell2 = row4.getCell(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            row4_cell2.setCellValue(sdf.format(check.getStarttime()));

            HSSFCell row4_cell5 = row4.getCell(4);
            row4_cell5.setCellValue(sdf.format(check.getEndtime()));

            // 第五行
            rownum++;
            HSSFRow row5 =  sheet.getRow(rownum);
            HSSFCell row5_cell2 = row5.getCell(1);
            row5_cell2.setCellValue(check.gethId());

            HSSFCell row5_cell5 = row5.getCell(4);
            row5_cell5.setCellValue(String.valueOf(check.getPayMoney()));

            // 第六行
            rownum++;
            HSSFRow row6 =  sheet.getRow(rownum);
            HSSFCell row6_cell5 = row6.getCell(4);
            row6_cell5.setCellValue(sdf.format(new Date()));

//            // 第七行
//            rownum++;
//            HSSFRow row7 =  sheet.getRow(rownum);
//
//            HSSFCell row7_cell5 = row7.getCell(4);
//            row7_cell5.setCellValue(carRentInfo.getOpenname());



            ByteArrayOutputStream fos = new ByteArrayOutputStream();
            workbook.write(fos);

            return fos;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  null;



    }
}

